/*
 *  each
 *  Calls a function for each item in an object
 */

objx.requires("objx.is", "objx.each");
 
objx.fn.each = function() {

	if (objx.debug) {
		if (!arguments[0]) {
			objx.error("each", "You must pass a function as the first argument to 'each'.");
		}
	}

	var obj = this.obj();

	if (this.is("string")) {
		// handle strings explicitly
		for (var i = 0; i < this.size(); i++) {
			if (arguments[0].apply(this, [obj.charAt(i), i]) === false)
				break;
		}
	} else if (this.is("number")) {
		// call each for every number
		for (var i = 0; i < obj; i++) {
			if (arguments[0].apply(this, [i, i]) === false)
				break;
		}
	} else if (this.is("array")) {
		// handle array like objects
		for (var i = 0; i < this.size(); i++) {
			if (arguments[0].apply(this, [obj[i], i]) === false)
				break;
		}
	} else {
		// handle every other kind of object
		for (var prop in obj) {
			if (arguments[0].apply(this, [obj[prop], prop]) === false)
				break;
		}
	}
	
	return this;
	
};

objx.provides("objx.each");